home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / flight-of-the-museum.swf / scripts / com / google / analytics / core / Buffer.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  11.8 KB  |  479 lines

  1. package com.google.analytics.core
  2. {
  3.    import com.google.analytics.data.UTMA;
  4.    import com.google.analytics.data.UTMB;
  5.    import com.google.analytics.data.UTMC;
  6.    import com.google.analytics.data.UTMK;
  7.    import com.google.analytics.data.UTMV;
  8.    import com.google.analytics.data.UTMZ;
  9.    import com.google.analytics.debug.DebugConfiguration;
  10.    import com.google.analytics.debug.VisualDebugMode;
  11.    import com.google.analytics.v4.Configuration;
  12.    import flash.events.NetStatusEvent;
  13.    import flash.net.SharedObject;
  14.    import flash.net.SharedObjectFlushStatus;
  15.    
  16.    public dynamic class Buffer
  17.    {
  18.        
  19.       
  20.       private var _SO:SharedObject;
  21.       
  22.       private var _OBJ:Object;
  23.       
  24.       private var _utma:UTMA;
  25.       
  26.       private var _utmb:UTMB;
  27.       
  28.       private var _utmc:UTMC;
  29.       
  30.       private var _debug:DebugConfiguration;
  31.       
  32.       private var _utmk:UTMK;
  33.       
  34.       private var _config:Configuration;
  35.       
  36.       private var _utmv:UTMV;
  37.       
  38.       private var _utmz:UTMZ;
  39.       
  40.       private var _volatile:Boolean;
  41.       
  42.       public function Buffer(config:Configuration, debug:DebugConfiguration, volatile:Boolean = false, data:Object = null)
  43.       {
  44.          var saveSO:Boolean = false;
  45.          var prop:String = null;
  46.          super();
  47.          _config = config;
  48.          _debug = debug;
  49.          UTMB.defaultTimespan = _config.sessionTimeout;
  50.          UTMZ.defaultTimespan = _config.conversionTimeout;
  51.          if(!volatile)
  52.          {
  53.             _SO = SharedObject.getLocal(_config.cookieName,_config.cookiePath);
  54.             saveSO = false;
  55.             if(_SO.data.utma)
  56.             {
  57.                if(!hasUTMA())
  58.                {
  59.                   _createUMTA();
  60.                }
  61.                _utma.fromSharedObject(_SO.data.utma);
  62.                if(_debug.verbose)
  63.                {
  64.                   _debug.info("found: " + _utma.toString(true),VisualDebugMode.geek);
  65.                }
  66.                if(_utma.isExpired())
  67.                {
  68.                   if(_debug.verbose)
  69.                   {
  70.                      _debug.warning("UTMA has expired",VisualDebugMode.advanced);
  71.                   }
  72.                   _clearUTMA();
  73.                   saveSO = true;
  74.                }
  75.             }
  76.             if(_SO.data.utmb)
  77.             {
  78.                if(!hasUTMB())
  79.                {
  80.                   _createUMTB();
  81.                }
  82.                _utmb.fromSharedObject(_SO.data.utmb);
  83.                if(_debug.verbose)
  84.                {
  85.                   _debug.info("found: " + _utmb.toString(true),VisualDebugMode.geek);
  86.                }
  87.                if(_utmb.isExpired())
  88.                {
  89.                   if(_debug.verbose)
  90.                   {
  91.                      _debug.warning("UTMB has expired",VisualDebugMode.advanced);
  92.                   }
  93.                   _clearUTMB();
  94.                   saveSO = true;
  95.                }
  96.             }
  97.             if(_SO.data.utmc)
  98.             {
  99.                delete _SO.data.utmc;
  100.                saveSO = true;
  101.             }
  102.             if(_SO.data.utmk)
  103.             {
  104.                if(!hasUTMK())
  105.                {
  106.                   _createUMTK();
  107.                }
  108.                _utmk.fromSharedObject(_SO.data.utmk);
  109.                if(_debug.verbose)
  110.                {
  111.                   _debug.info("found: " + _utmk.toString(),VisualDebugMode.geek);
  112.                }
  113.             }
  114.             if(_SO.data.utmv)
  115.             {
  116.                if(!hasUTMV())
  117.                {
  118.                   _createUMTV();
  119.                }
  120.                _utmv.fromSharedObject(_SO.data.utmv);
  121.                if(_debug.verbose)
  122.                {
  123.                   _debug.info("found: " + _utmv.toString(true),VisualDebugMode.geek);
  124.                }
  125.                if(_utmv.isExpired())
  126.                {
  127.                   if(_debug.verbose)
  128.                   {
  129.                      _debug.warning("UTMV has expired",VisualDebugMode.advanced);
  130.                   }
  131.                   _clearUTMV();
  132.                   saveSO = true;
  133.                }
  134.             }
  135.             if(_SO.data.utmz)
  136.             {
  137.                if(!hasUTMZ())
  138.                {
  139.                   _createUMTZ();
  140.                }
  141.                _utmz.fromSharedObject(_SO.data.utmz);
  142.                if(_debug.verbose)
  143.                {
  144.                   _debug.info("found: " + _utmz.toString(true),VisualDebugMode.geek);
  145.                }
  146.                if(_utmz.isExpired())
  147.                {
  148.                   if(_debug.verbose)
  149.                   {
  150.                      _debug.warning("UTMZ has expired",VisualDebugMode.advanced);
  151.                   }
  152.                   _clearUTMZ();
  153.                   saveSO = true;
  154.                }
  155.             }
  156.             if(saveSO)
  157.             {
  158.                save();
  159.             }
  160.          }
  161.          else
  162.          {
  163.             _OBJ = new Object();
  164.             if(data)
  165.             {
  166.                for(prop in data)
  167.                {
  168.                   _OBJ[prop] = data[prop];
  169.                }
  170.             }
  171.          }
  172.          _volatile = volatile;
  173.       }
  174.       
  175.       public function clearCookies() : void
  176.       {
  177.          utma.reset();
  178.          utmb.reset();
  179.          utmc.reset();
  180.          utmz.reset();
  181.          utmv.reset();
  182.          utmk.reset();
  183.       }
  184.       
  185.       public function save() : void
  186.       {
  187.          var flushStatus:String = null;
  188.          if(!isVolatile())
  189.          {
  190.             flushStatus = null;
  191.             try
  192.             {
  193.                flushStatus = _SO.flush();
  194.             }
  195.             catch(e:Error)
  196.             {
  197.                _debug.warning("Error...Could not write SharedObject to disk");
  198.             }
  199.             switch(flushStatus)
  200.             {
  201.                case SharedObjectFlushStatus.PENDING:
  202.                   _debug.info("Requesting permission to save object...");
  203.                   _SO.addEventListener(NetStatusEvent.NET_STATUS,_onFlushStatus);
  204.                   break;
  205.                case SharedObjectFlushStatus.FLUSHED:
  206.                   _debug.info("Value flushed to disk.");
  207.             }
  208.          }
  209.       }
  210.       
  211.       public function get utmv() : UTMV
  212.       {
  213.          if(!hasUTMV())
  214.          {
  215.             _createUMTV();
  216.          }
  217.          return _utmv;
  218.       }
  219.       
  220.       public function get utmz() : UTMZ
  221.       {
  222.          if(!hasUTMZ())
  223.          {
  224.             _createUMTZ();
  225.          }
  226.          return _utmz;
  227.       }
  228.       
  229.       public function hasUTMA() : Boolean
  230.       {
  231.          if(_utma)
  232.          {
  233.             return true;
  234.          }
  235.          return false;
  236.       }
  237.       
  238.       public function hasUTMB() : Boolean
  239.       {
  240.          if(_utmb)
  241.          {
  242.             return true;
  243.          }
  244.          return false;
  245.       }
  246.       
  247.       public function hasUTMC() : Boolean
  248.       {
  249.          if(_utmc)
  250.          {
  251.             return true;
  252.          }
  253.          return false;
  254.       }
  255.       
  256.       public function isGenuine() : Boolean
  257.       {
  258.          if(!hasUTMK())
  259.          {
  260.             return true;
  261.          }
  262.          return utmk.hash == generateCookiesHash();
  263.       }
  264.       
  265.       public function resetCurrentSession() : void
  266.       {
  267.          _clearUTMB();
  268.          _clearUTMC();
  269.          save();
  270.       }
  271.       
  272.       public function hasUTMK() : Boolean
  273.       {
  274.          if(_utmk)
  275.          {
  276.             return true;
  277.          }
  278.          return false;
  279.       }
  280.       
  281.       public function generateCookiesHash() : Number
  282.       {
  283.          var value:String = "";
  284.          value += utma.valueOf();
  285.          value += utmb.valueOf();
  286.          value += utmc.valueOf();
  287.          value += utmz.valueOf();
  288.          value += utmv.valueOf();
  289.          return Utils.generateHash(value);
  290.       }
  291.       
  292.       private function _createUMTB() : void
  293.       {
  294.          _utmb = new UTMB();
  295.          _utmb.proxy = this;
  296.       }
  297.       
  298.       private function _createUMTC() : void
  299.       {
  300.          _utmc = new UTMC();
  301.       }
  302.       
  303.       private function _createUMTA() : void
  304.       {
  305.          _utma = new UTMA();
  306.          _utma.proxy = this;
  307.       }
  308.       
  309.       public function hasUTMV() : Boolean
  310.       {
  311.          if(_utmv)
  312.          {
  313.             return true;
  314.          }
  315.          return false;
  316.       }
  317.       
  318.       private function _createUMTK() : void
  319.       {
  320.          _utmk = new UTMK();
  321.          _utmk.proxy = this;
  322.       }
  323.       
  324.       public function hasUTMZ() : Boolean
  325.       {
  326.          if(_utmz)
  327.          {
  328.             return true;
  329.          }
  330.          return false;
  331.       }
  332.       
  333.       private function _createUMTV() : void
  334.       {
  335.          _utmv = new UTMV();
  336.          _utmv.proxy = this;
  337.       }
  338.       
  339.       private function _onFlushStatus(event:NetStatusEvent) : void
  340.       {
  341.          _debug.info("User closed permission dialog...");
  342.          switch(event.info.code)
  343.          {
  344.             case "SharedObject.Flush.Success":
  345.                _debug.info("User granted permission -- value saved.");
  346.                break;
  347.             case "SharedObject.Flush.Failed":
  348.                _debug.info("User denied permission -- value not saved.");
  349.          }
  350.          _SO.removeEventListener(NetStatusEvent.NET_STATUS,_onFlushStatus);
  351.       }
  352.       
  353.       private function _createUMTZ() : void
  354.       {
  355.          _utmz = new UTMZ();
  356.          _utmz.proxy = this;
  357.       }
  358.       
  359.       public function updateUTMA(timestamp:Number) : void
  360.       {
  361.          if(_debug.verbose)
  362.          {
  363.             _debug.info("updateUTMA( " + timestamp + " )",VisualDebugMode.advanced);
  364.          }
  365.          if(!utma.isEmpty())
  366.          {
  367.             if(isNaN(utma.sessionCount))
  368.             {
  369.                utma.sessionCount = 1;
  370.             }
  371.             else
  372.             {
  373.                utma.sessionCount += 1;
  374.             }
  375.             utma.lastTime = utma.currentTime;
  376.             utma.currentTime = timestamp;
  377.          }
  378.       }
  379.       
  380.       private function _clearUTMA() : void
  381.       {
  382.          _utma = null;
  383.          if(!isVolatile())
  384.          {
  385.             _SO.data.utma = null;
  386.             delete _SO.data.utma;
  387.          }
  388.       }
  389.       
  390.       private function _clearUTMC() : void
  391.       {
  392.          _utmc = null;
  393.       }
  394.       
  395.       private function _clearUTMB() : void
  396.       {
  397.          _utmb = null;
  398.          if(!isVolatile())
  399.          {
  400.             _SO.data.utmb = null;
  401.             delete _SO.data.utmb;
  402.          }
  403.       }
  404.       
  405.       public function update(name:String, value:*) : void
  406.       {
  407.          if(isVolatile())
  408.          {
  409.             _OBJ[name] = value;
  410.          }
  411.          else
  412.          {
  413.             _SO.data[name] = value;
  414.          }
  415.       }
  416.       
  417.       private function _clearUTMZ() : void
  418.       {
  419.          _utmz = null;
  420.          if(!isVolatile())
  421.          {
  422.             _SO.data.utmz = null;
  423.             delete _SO.data.utmz;
  424.          }
  425.       }
  426.       
  427.       private function _clearUTMV() : void
  428.       {
  429.          _utmv = null;
  430.          if(!isVolatile())
  431.          {
  432.             _SO.data.utmv = null;
  433.             delete _SO.data.utmv;
  434.          }
  435.       }
  436.       
  437.       public function isVolatile() : Boolean
  438.       {
  439.          return _volatile;
  440.       }
  441.       
  442.       public function get utma() : UTMA
  443.       {
  444.          if(!hasUTMA())
  445.          {
  446.             _createUMTA();
  447.          }
  448.          return _utma;
  449.       }
  450.       
  451.       public function get utmb() : UTMB
  452.       {
  453.          if(!hasUTMB())
  454.          {
  455.             _createUMTB();
  456.          }
  457.          return _utmb;
  458.       }
  459.       
  460.       public function get utmc() : UTMC
  461.       {
  462.          if(!hasUTMC())
  463.          {
  464.             _createUMTC();
  465.          }
  466.          return _utmc;
  467.       }
  468.       
  469.       public function get utmk() : UTMK
  470.       {
  471.          if(!hasUTMK())
  472.          {
  473.             _createUMTK();
  474.          }
  475.          return _utmk;
  476.       }
  477.    }
  478. }
  479.